﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProcurementManagement.Applcation.Command.ProcurementReturnCommand;
using ProcurementManagement.Applcation.Command.PurchaseContractsCommand;
using ProcurementManagement.Applcation.Command.VendorCommand;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Domain.Dtos.PurchaseContracts;
using ProcurementManagement.Dtos;
using ProcurementManagement.Infrstructre;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProcurementManagement.Controllers
{
    /// <summary>
    /// 采购合同控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class PurchaseContractsController : ControllerBase
    {
        protected readonly IMediator mediatorp;

        public PurchaseContractsController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }
        /// <summary>
        /// 采购合同添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPurchaseContracts(PurchaseContractsCreateCommand command)
        {
            ApiResult<int> result=new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.AddPurchaseContractsDTO.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddPurchaseContractsDTO.AddName = name;
                command.AddPurchaseContractsDTO.AddTime = DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.AddPurchaseContractsDTO.User_Id = int.Parse(userId);
                result.Data=await mediatorp.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        ///采购合同列表
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<SelectpurchaseContractDTO>>> ProductTypeSelectShow([FromQuery] PurchaseContractsSelectCommand command)
        {
            ApiResult<PageDTO<SelectpurchaseContractDTO>> result = new ApiResult<PageDTO<SelectpurchaseContractDTO>>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 采购合同删除
        /// </summary>
        /// <param name="command">要删除的信息id</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelPurchaseContract(PurchaseContractsDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelName = name;
                command.DelTime = DateTime.Now;
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>
        ///采购合同反填
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<FanPurchaseContractsDTO>>> FanPurchaseContracts([FromQuery] PurchaseContractsFanCommand command)
        {
            ApiResult<List<FanPurchaseContractsDTO>> result = new ApiResult<List<FanPurchaseContractsDTO>>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改采购合同
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdPurchaseContracts(UpdPurchaseContractsDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //获取用户ID
                command.Office_Id = sid;
                command.User_Id = int.Parse(userId);
                result.Data = await mediatorp.Send(new PurchaseContractsUpdCommand { UpdDTO = command });
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填(2)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<FanTianPurchaseContractsDTO>> FanProcurementReturn([FromQuery] FanPurchaseContractsCommand command)
        {
            ApiResult<FanTianPurchaseContractsDTO> result = new ApiResult<FanTianPurchaseContractsDTO>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
    }
}
